<?php
class AttrConfFormProcessor
{
	static public function standardize($post)
	{
		if(!isset($post["disabled"]))
		{
			$post["disabled"] = 0;
		}
		if (in_array($post["form_item_type"], array("radio", "select", "checkbox", "multiple_select")))
		{
			$maxOptionIndex = 0;
			$maxDisplayOrder = 0;
			$options = array();
			$defaultChecked = array();
			$optionWithoutIndex = array();
			foreach($post["options"] as $key => $option)
			{
				if (empty($option["option_index"]))
				{
					$optionWithoutIndex[$key] = $option;
				}
				else
				{
					$options[] = $option;
					if (isset($post["default_checked"]) && in_array($key, $post["default_checked"]))
					{
						$defaultChecked[] = $option["option_index"];
					}
					$maxOptionIndex = $maxOptionIndex < $option["option_index"] ? $option["option_index"] : $maxOptionIndex;
					$maxDisplayOrder = $maxDisplayOrder < $option["display_order"] ? $option["display_order"] : $maxDisplayOrder;
				}
			}
			foreach($optionWithoutIndex as $key => $option)
			{				
				$maxOptionIndex ++;
				$maxDisplayOrder ++;
				$option["option_index"] = $maxOptionIndex;
				$option["display_order"] = $maxDisplayOrder;
				if (isset($post["default_checked"]) && in_array($key, $post["default_checked"]))
				{
					$defaultChecked[] = $option["option_index"];
				}
				$options[] = $option;
			}
			$post["options"] = $options;
			if (isset($post["default_value"]) && count($post["default_value"]))
			{
				$post["default_value"] = in_array($post["form_item_type"], array("checkbox", "multiple_select")) ? $defaultChecked : $defaultChecked[0];
			}
			else
			{
				$post["default_value"] = null;
			}
			unset($post["validator_conf"]["rules"]["ban"], $post["validator_conf"]["rules"]["mask"]
			, $post["validator_conf"]["explain"]["ban"], $post["validator_conf"]["explain"]["mask"]
			, $post["validator_conf"]["rules"]["min_value"], $post["validator_conf"]["rules"]["max_value"]
			, $post["validator_conf"]["explain"]["min_value"], $post["validator_conf"]["explain"]["max_value"]
			, $post["validator_conf"]["rules"]["min_length"], $post["validator_conf"]["rules"]["max_length"]
			, $post["validator_conf"]["explain"]["min_length"], $post["validator_conf"]["explain"]["max_length"]);
			if (in_array($post["form_item_type"], array("radio", "select")))
			{
				unset($post["validator_conf"]["rules"]["min_selected"], $post["validator_conf"]["rules"]["max_selected"]
			, $post["validator_conf"]["explain"]["min_selected"], $post["validator_conf"]["explain"]["max_selected"]);
			}
		}
		else
		{
			$post["default_value"] = $post["default_text"];
			$post["filterable"] = 0;
			unset($post["default_text"], $post["options"]
			, $post["validator_conf"]["rules"]["min_selected"], $post["validator_conf"]["rules"]["max_selected"]
			, $post["validator_conf"]["explain"]["min_selected"], $post["validator_conf"]["explain"]["max_selected"]);
		}
		unset($post["default_checked"], $post["default_text"]);
		return $post;
	}
}